/**
  * 本程序是为了创建DayuDict需要使用的Sqlite3数据库而使用的一个小工具。
  * 本程序使用build进行编译，build需要的createDayuDict_db.brf内容如下：
  *  createDayuDict_db.d  要编译的文件
	*	 sqlite.lib           编译需要的lib文件
	*	 -Dddoc               产生帮助文档
  * Authors:<font color="blue">曹子玉</font> <a href="mailto:jinheking@gmail.com">jinheking@gmail.com</a>. <a href="http://code.google.com/p/dayudict/">项目主站</a>
  * Date: April 23, 2007
  * License:GPL 2.0
  * Version:0.02
  * Date: April 24, 2007  <font color="blue">添加创建索引功能，create index dict_idx on dict(word asc);</font>
  */  
module createDayuDict_db;
import sqlite3_imp;
import std.stdio;





/** 
	* 回叫函数。*
  * Params:
  *	NotUsed =	一般用不到。
  *	numCols =	总列数
  * results = 返回值
  * columnNames = 列名
  */
int DoTheCallback (void* NotUsed, int numCols , char[][] results,char[][] columnNames){
	writefln("number of colomns: ", numCols, "\n");
	return 0;
};
/**
  * build时候使用的设置。
  * 为了产生帮助文档和指定编译的可执行文件的文件名。
  */
version(build){
	pragma(include, "d.ddoc"); 
	pragma(target, "createDayuDictDB.exe"); 
}

/++++++++++++++++++++++++
  + 创建数据库，创建表
  + 词库表的结构很简单，用 SQL 语言描述： TABLE dictionary(word TEXT, phonetics TEXT, translation TEXT); 
  + Example:
  + --------------------------
  +  import std.stdio;
  +  import import sqlite3_imp;
  +
  +  void foo()
  +  {
  +     sqlite3* db;
	+			int code;	
	+			code = sqlite3_open("dayudict.db", &db);
	+			if(SQLITE_OK != code){
	+				printf("DB create error: %s\n", sqlite3_errmsg(db));
	+				return 1;
	+			}
	+			printf("DB open!\n");	
	+			
	+			code = sqlite3_exec(db, "create table dict(word TEXT, phonetics TEXT, translation TEXT);" ,cast(sqlite_callback) &DoTheCallback, null, null);
	+			
	+			if(SQLITE_OK != code){
	+				printf("DB create error: %s\n", sqlite3_errmsg(db));
	+				return 1;
	+			}
	+			
	+			sqlite3_close(db);
	+			printf("DB closed.\n");
	+			printf("DayuDict's database had created.\n");
	+			return 0;
  +  }
  + --------------------------
  +/
int main(){
	sqlite3* db;
	int code;
	
	code = sqlite3_open("dayudict.db", &db);
	if(SQLITE_OK != code){
		printf("DB create error: %s\n", sqlite3_errmsg(db));
		return 1;
	}
	printf("DB open!\n");	
	
	code = sqlite3_exec(db, "create table dict(word TEXT, phonetics TEXT, translation TEXT);" ,cast(sqlite_callback) &DoTheCallback, null, null);
	
	if(SQLITE_OK != code){
		printf("DB create error: %s\n", sqlite3_errmsg(db));
		return 1;
	}
	
	///创建索引
	 
	code = sqlite3_exec(db, "create index dict_idx on dict(word asc);" ,cast(sqlite_callback) &DoTheCallback, null, null);
	
	if(SQLITE_OK != code){
		printf("DB create error: %s\n", sqlite3_errmsg(db));
		return 1;
	}
	sqlite3_close(db);
	printf("DB closed.\n");
	printf("DayuDict's database had created.\n");
	return 0;
}